<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="密码" prop="password">-->
<!--        <el-input-->
<!--          v-model="queryParams.password"-->
<!--          placeholder="请输入密码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="公司名字" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录名" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入登录名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="responsiblePerson">
        <el-input
          v-model="queryParams.responsiblePerson"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入电子邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="email">
        <el-select
          v-model="queryParams.status"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option label="未审核" value="0"></el-option>
          <el-option label="审核不通过" value="1"></el-option>
          <el-option label="审核通过" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:comuser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:comuser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:comuser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:comuser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="comuserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="该表的id" align="center" prop="id" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="公司名字" align="center" prop="companyName" />
      <el-table-column label="账户状态" align="center" prop="status" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="登录名" align="center" prop="loginName" />
      <el-table-column label="负责人" align="center" prop="responsiblePerson" />
      <el-table-column label="电子邮箱" align="center" prop="email" />
      <el-table-column label="单位类型" align="center" prop="unitType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.unittype" :value="scope.row.unitType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePass(scope.row)"
            v-hasPermi="['system:user:add','system:dept:add']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleNoPass(scope.row)"
            v-hasPermi="['system:comuser:edit']"
          >不通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:comuser:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司用户注册对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="公司名字" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名字" />
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入登录名" />
        </el-form-item>
        <el-form-item label="负责人" prop="responsiblePerson">
          <el-input v-model="form.responsiblePerson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComuser, getComuser, delComuser, addComuser, updateComuser } from "@/api/system/comuser";
import { listDept,addDept,delDept } from "@/api/system/dept";

import { addComUser } from "@/api/system/user";

export default {
  name: "Comuser",
  dicts: ['unittype'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司用户注册表格数据
      comuserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        password: null,
        companyName: null,
        status: 0,
        phonenumber: null,
        loginName: null,
        responsiblePerson: null,
        email: null
      },
      // 表单参数
      form: {
        status:0,
      },
      //部门参数
      deptForm:{

      },
      //用户参数
      userForm:{

      },
      //部门数据
      deptData:null,
      // 表单校验
      rules: {
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        companyName: [
          { required: true, message: "公司名字不能为空", trigger: "blur" }
        ],
        loginName: [
          { required: true, message: "登录名不能为空", trigger: "blur" }
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],

        phonenumber: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公司用户注册列表 */
    getList() {
      this.loading = true;
      listComuser(this.queryParams).then(response => {
        this.comuserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        password: null,
        companyName: null,
        status: 0,
        phonenumber: null,
        loginName: null,
        responsiblePerson: null,
        email: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司用户注册";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getComuser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司用户注册";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateComuser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComuser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除公司用户注册编号为"' + ids + '"的数据项？').then(function() {
        return delComuser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/comuser/export', {
        ...this.queryParams
      }, `comuser_${new Date().getTime()}.xlsx`)
    },
    /** 通过的按钮操作 */
    handlePass(row) {
      this.reset();
      const id = row.id || this.ids
      let deptId=242+parseInt(row.unitType);
      let roleId=99+parseInt(row.unitType);
      this.deptForm.parentId=deptId;
      this.deptForm.deptName=row.companyName
      this.deptForm.orderNum=1;
      this.deptForm.email=row.email;
      this.deptForm.phone=row.phonenumber;
      this.deptForm.status=0;
      this.deptForm.leader=row.responsiblePerson;
      this.userForm.email=row.email;
      this.userForm.phonenumber=row.phonenumber;
      this.userForm.password=row.password;
      this.userForm.userName=row.loginName;
      this.userForm.nickName=row.responsiblePerson;
      this.userForm.status=0;
      this.userForm.roleIds=[roleId];
      console.log(row.unitType);
      console.log(this.deptForm.parentId);
      addDept(this.deptForm).then(async response => {
        await listDept().then(response => {
          this.deptData=response.data;
        });
        for(let a in this.deptData){
          console.log(this.deptData[a]);
          if(this.deptData[a].deptName===row.companyName){
            this.userForm.deptId=this.deptData[a].deptId;
            console.log("找到");
            break;
          }
        }
        console.log(this.userForm.deptId);
        addComUser(this.userForm).then(response=>{
          console.log("返回状态"+response.code)
          row.status=2;
          updateComuser(row).then(() => {
            this.$modal.msgSuccess("审核通过");
            this.getList();
          });
        }).catch(() => {
          delDept(this.userForm.deptId);
          this.$modal.msgSuccess("数据重复或错误，无法通过");
        })
      });
    },
    /** 不通过的按钮操作 */
    handleNoPass(row) {
      this.reset();
      const id = row.id || this.ids
      row.status=1;
      updateComuser(row).then(() => {
        this.$modal.msgSuccess("审核不通过");
      });
    },
  }
};
</script>
